﻿using AppSocketModel;
using System;
using System.Collections.Generic;
using System.Text;
using BCGPSCommon;
using LJD.IMS.Utility;
using AppSocketService.Vehicle;
using BCGPSMongoData; 
using AppSocketService.Location;
using MongoDB.Driver;

namespace AppSocketService.Account
{
    public  class SystemAccountService : ServiceBase, ISystemAccountService
    {
        private readonly MongoDBCommand<SystemAccountDto> mongoDBCommand = new MongoDBCommand<SystemAccountDto>("SystemAccount");
        private readonly IRedisService redisService;



        public SystemAccountService(IClaimsAccessor claimsAccesso, IRedisService redisService) : base(claimsAccesso)
        {
            this.redisService = redisService;
        }

        #region 登录
        public SystemAccountDto Login(SystemAccountLoginDto accountLoginDto, string ip)
        {
            var dto = mongoDBCommand.SelectDocumentAsync(s => s.SystemAccountCode == accountLoginDto.AccountCode).GetAwaiter().GetResult();
            if (dto == null)
                throw new ArgumentException("登录账户不存在");
            if (accountLoginDto.Password.SHA256(accountLoginDto.AccountCode) != dto.Password)
                throw new ArgumentException("账户密码不正确!");


            var update = GetUpdateDefinition(dto, ip);
            var accountDto = mongoDBCommand.UpdateDocumentAsync(s => s.SystemAccountCode == accountLoginDto.AccountCode, update).GetAwaiter().GetResult();
            return accountDto;

        }

        private UpdateDefinition<SystemAccountDto> GetUpdateDefinition(SystemAccountDto dto, string ip)
        {

            return Builders<SystemAccountDto>.Update
                .Set(s => s.LastLoginTime, DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc))
                .Set(s => s.LoginTimes, dto.LoginTimes + 1)
                .Set(s => s.LoginIp, ip);
        }
        #endregion

        #region 系统账户号注册
        public SystemAccountDto RegisterSystemAccount(SystemAccountRegisterDto dto)
        { 
            if(dto.SystemAccountCode.Length < 6)
                    throw new ArgumentException("账户号要大于6个字符!");

            var accountDto = mongoDBCommand.SelectDocumentAsync(s => s.AccountName == dto.AccountName  ).GetAwaiter().GetResult();

            if (accountDto != null)
                throw new ArgumentException("账户名字已经被注册!");


            accountDto = mongoDBCommand.SelectDocumentAsync(s => s.SystemAccountCode == dto.SystemAccountCode).GetAwaiter().GetResult();

            if (accountDto != null)
                throw new ArgumentException("账户号已经被注册!");

            if (string.IsNullOrWhiteSpace(dto.Password))
                throw new ArgumentException("请输入密码!");

            if (dto.Password.Trim().Length < 4)
                throw new ArgumentException("密码长度不能小于4个字符!");

            accountDto = dto.As<SystemAccountDto>(); 
            accountDto.Password = accountDto.Password.SHA256(accountDto.SystemAccountCode);
            accountDto.CreateTime = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);

           mongoDBCommand.InsertDocumentAsync(accountDto).GetAwaiter().GetResult();

            accountDto.Password = null;
            return accountDto;
        }
        #endregion


        #region 获取当前登录账户
        public SystemAccountDto GetCurrentAccount()
        {
            return mongoDBCommand.SelectDocumentAsync(s => s.Id == claimsAccessor.AccountId).GetAwaiter().GetResult();
        }
        #endregion
    }
}
